//
//  QBAssetCell.m
//  QBImagePicker
//
//  Created by Katsuma Tanaka on 2015/04/06.
//  Copyright (c) 2015 Katsuma Tanaka. All rights reserved.
//

#import "SimAssetCell.h"
#import "UIButton+SimAdditions.h"
#import "UIView+SimAdditions.h"

SimImpValue(kPickerAssetUnSelectedIcon, NSString *)
SimImpValue(kPickerAssetSelectedIcon, NSString *)

@interface SimVideoPlayIconView : UIView

@property (nonatomic, strong) UIColor *iconColor;

@end


@implementation SimVideoPlayIconView

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.iconColor = [UIColor whiteColor];
}

- (void)drawRect:(CGRect)rect
{
    [self.iconColor setFill];
    UIBezierPath *trianglePath = [UIBezierPath bezierPath];
    [trianglePath moveToPoint:CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMinY(self.bounds))];
    [trianglePath addLineToPoint:CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMaxY(self.bounds))];
    [trianglePath addLineToPoint:CGPointMake(CGRectGetMaxX(self.bounds) - CGRectGetMidY(self.bounds), CGRectGetMidY(self.bounds))];
    [trianglePath closePath];
    [trianglePath fill];
    
    UIBezierPath *squarePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds), CGRectGetWidth(self.bounds) - CGRectGetMidY(self.bounds) - 1.0, CGRectGetHeight(self.bounds)) cornerRadius:2.0];
    [squarePath fill];
}
@end

@interface SimVideoBar : UIView

@end

@implementation SimVideoBar

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Add gradient layer
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.colors = @[
                             (__bridge id)[[UIColor clearColor] CGColor],
                             (__bridge id)[[UIColor blackColor] CGColor]
                             ];
    
    [self.layer insertSublayer:gradientLayer atIndex:0];
}

@end


@interface SimAssetCell ()
{
    UILabel *_vedioTimeLb;
}

@end

@implementation SimAssetCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:_imageView];
        
        NSString *unSelectedImageStr = SimValue(kPickerAssetUnSelectedIcon, @"ImageSelectedOff");
        UIImage *selectedImage = SimUIImageNamed(SimValue(kPickerAssetSelectedIcon, @"ImageSelectedOn"));
        _checkBtn = [UIButton btnWithImageNames:@[unSelectedImageStr] btnSize:CGSizeMake(50, 50)];
        [_checkBtn setImage:selectedImage forState:UIControlStateSelected];
        [_checkBtn addTarget:self action:@selector(checkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_checkBtn];
        
        _videoView = [[SimVideoBar alloc] initWithFrame:CGRectMake(0, 0, self.width, 20)];
        [self.contentView addSubview:_videoView];
        _videoView.hidden = YES;
        
        SimVideoPlayIconView *playIconView = [[SimVideoPlayIconView alloc] initWithFrame:CGRectMake(0, 0, 14, 8)];
        playIconView.left = 5;
        playIconView.centerY = _videoView.height/2;
        [_videoView addSubview:playIconView];
        
        _vedioTimeLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, playIconView.right+5, _videoView.width-(playIconView.right+5))];
        _vedioTimeLb.font = [UIFont systemFontOfSize:12];
        _vedioTimeLb.textColor = [UIColor whiteColor];
        _vedioTimeLb.backgroundColor = [UIColor clearColor];
        [_videoView addSubview:_vedioTimeLb];
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _imageView.frame = self.bounds;
    _videoView.bottom = _imageView.height;
    
    _checkBtn.frame = CGRectInset(_imageView.bounds, 20, 20);
    _checkBtn.left = _imageView.width/2;
    _checkBtn.bottom = _imageView.height/2;
}


- (void)checkBtnClick:(UIButton *)btn
{
    if (_clickOnCheckBtn) {
        _clickOnCheckBtn(btn);
    }
}

- (void)setVideoTime:(NSString *)videoTime
{
    _vedioTimeLb.text = videoTime;
}

@end
